package com.itheima.module.web.controller.mvc;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/mvc")
public class MVCController {

    //当请求来的以后  希望返回 当前时间
    @RequestMapping("/time1")
    public void currentTime(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {

        Date date = new Date();

        //展示跟我没关系
        request.setAttribute("time",date);

        //交给jsp去吧
        request.getRequestDispatcher("/pages/time.jsp").forward(request,response);
    }


    @RequestMapping("/time2")
    public ModelAndView currentTime2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        Date date = new Date();

        //表示数据和jsp路径 合成对象
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("time",date);


        modelAndView.setViewName("/pages/time.jsp");
        return modelAndView;
    }

    @RequestMapping("/time3")
    public ModelAndView currentTime3(ModelAndView modelAndView) throws IOException, ServletException {

        Date date = new Date();

        //表示数据和jsp路径 合成对象
        modelAndView.addObject("time",date);


        modelAndView.setViewName("time");
        return modelAndView;
    }
    @RequestMapping("/time4")
    public String currentTime4(Model model) throws IOException, ServletException {

        Date date = new Date();


        model.addAttribute("time",date);
        return "/pages/time.jsp";
    }

    /**
     * 注意这里需要配置 前缀和后缀了
     * <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     *         <property name="prefix" value="/pages/"></property>
     *         <property name="suffix" value=".jsp"></property>
     *
     *     </bean>
     *
     * @param model
     * @return
     * @throws IOException
     * @throws ServletException
     */
    @RequestMapping("/time5")
    public String currentTime5(Model model) throws IOException, ServletException {

        Date date = new Date();

        model.addAttribute("time",date);

        return "time";
    }


    @RequestMapping("/time6")
    public void currentTime6(HttpServletResponse response) throws IOException, ServletException {

        HashMap<Object, Object> map = new HashMap<>();
        map.put("time",new Date());

        String s = JSON.toJSONString(map);
        response.getWriter().print(s);
    }

    /**
     * 注意这里要导入依赖
     *  <dependency>
     *       <groupId>com.fasterxml.jackson.core</groupId>
     *       <artifactId>jackson-databind</artifactId>
     *       <version>2.12.4</version>
     *     </dependency>
     *
     * @return
     * @throws IOException
     * @throws ServletException
     */
    @RequestMapping("/time7")
    @ResponseBody
    public Map currentTime7() throws IOException, ServletException {

        HashMap<Object, Object> map = new HashMap<>();
        map.put("time",new Date());

        return map;
    }


}
